package com.supervision.student;

import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.LocationSource;

import org.w3c.dom.ls.LSOutput;

import java.net.URL;

import okhttp3.OkHttpClient;
import okhttp3.Request;

import static java.lang.Thread.sleep;

public class PositioningService extends Service implements LocationSource, AMapLocationListener {

    //获取经纬度
    private double Latitude = 0.0, Longitude = 0.0;

    String webIp,phone;

    //定位需要的声明
    private AMapLocationClient mLocationClient = null;//定位发起端
    private AMapLocationClientOption mLocationOption = null;//定位参数
    private OnLocationChangedListener mListener = null;//定位监听器

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        webIp = intent.getStringExtra("webIp");
        phone = intent.getStringExtra("phone");
        //开始定位
        initLoc();
        return super.onStartCommand(intent, flags, startId);
    }

    public PositioningService() {



        System.out.println(Latitude + "  " + Longitude);
        PositionAT positionAT = new PositionAT();
        positionAT.execute();


    }

    class PositionAT extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... voids) {
            OkHttpClient okHttpClient = new OkHttpClient();//创建单例
            URL url;
            Request request;//创建请求
            while (true) {
                try {
                    sleep(2000);
                    //设置url
                    url = new URL("http://" + webIp + ":8080/student/" + phone + "/" + Latitude + "/" + Longitude);
                    //设置请求
                    request = new Request.Builder()
                            .url(url)
                            .build();
                    //执行请求
                    okHttpClient.newCall(request).execute();

                } catch (Exception e) {
                }
            }
        }

    }

    //定位
    private void initLoc() {

        //初始化定位
        mLocationClient = new AMapLocationClient(getApplication());
        //设置定位回调监听
        mLocationClient.setLocationListener(this);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(false);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(true);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }

    //定位回调
    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                //定位成功回调信息，设置相关消息
                Latitude = aMapLocation.getLatitude();//获取纬度
                Longitude = aMapLocation.getLongitude();//获取经度
            } else {
                Latitude = 0.0;
                Longitude = 0.0;
                System.out.println(321);
                Log.e("AmapError","location Error, ErrCode:"
                        + aMapLocation.getErrorCode() + ", errInfo:"
                        + aMapLocation.getErrorInfo());
            }
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    //激活定位
    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mListener = onLocationChangedListener;
    }

    //停止定位
    @Override
    public void deactivate() {
        mListener = null;
    }
}
